import pyPokeys56U as POKEYS56U
import time
from version import version

def test_setup():
    """ """
    print 'setup in progress...'

def test_output():
    """  """
    poKeys56U = POKEYS56U.pokeys56U_USBDevice()
    poKeys56U.open()

    print '-- GPIO Function --'
    pinSetting = poKeys56U.GetPinData(0x2A, 0x12)
    print ('Pin 43', pinSetting)
    PinData = poKeys56U.SetPinData(0x2A, ["Digital output"], 0x13)
    pinSetting = poKeys56U.GetPinData(0x2A, 0x14)
    print ('Pin 43', pinSetting)
        
    print '-- BLINK LED : SET --'
    pinSetting = poKeys56U.SetOutput(0x2A, 0x00, 0x15)
    time.sleep(2)
    pinSetting = poKeys56U.SetOutput(0x2A, 0x01, 0x16)
    time.sleep(2)
    pinSetting = poKeys56U.SetOutput(0x2A, 0x00, 0x17)
    time.sleep(2)
    pinSetting = poKeys56U.SetOutput(0x2A, 0x01, 0x18)
    time.sleep(2)
    pinSetting = poKeys56U.SetOutput(0x2A, 0x00, 0x19)

    poKeys56U.close()

def test_input():
    """  """
    poKeys56U = POKEYS56U.pokeys56U_USBDevice()
    poKeys56U.open()

    print '-- GPIO Function --'
    pinSetting = poKeys56U.GetPinData(0x29, 0x1A)
    print ('Pin 42', pinSetting)
    PinData = poKeys56U.SetPinData(0x29, ["Digital input"], 0x1B)
    pinSetting = poKeys56U.GetPinData(0x29, 0x1C)
    print ('Pin 42', pinSetting)
        
    print '-- PUSH BUTTON : GET --'
    pinValue = poKeys56U.GetInput(0x29, 0x1D)
    print pinValue

    print '-- GET INPUT (1 to 32) --'
    pinValue = poKeys56U.BlockGetInput1(0x01, 0x1E)
    print pinValue

    print '-- GET INPUT (33 to 55)  --'
    pinValue = poKeys56U.BlockGetInput2(0x02, 0x1F)
    print pinValue

    print '-- GET INPUT (1 to 55)  --'
    pinValue = poKeys56U.GetInput(0x20)
    print pinValue

    poKeys56U.close()

def test_gpio_function():
    """  """
    poKeys56U = POKEYS56U.pokeys56U_USBDevice()
    poKeys56U.open()
    print '-- GPIO Function --'
    for i in range(5): # Max 55
        pinSetting = poKeys56U.GetPinData(0x00+i, 0x09+i)
        print ('Pin ', i, pinSetting)
        
    print '-- GPIO : SET --'
    PinData = poKeys56U.SetPinData(0x04, ["Digital input", "Invert state"], 0x0F)
    print PinData
    print '-- Saving Configuration --'
    save = poKeys56U.SaveConfig(0x11)
    print save
        
    print '-- GPIO : GET --'
    pinSetting = poKeys56U.GetPinData(0x04, 0x10)
    print pinSetting
    poKeys56U.close()

def test_user_id():
    poKeys56U = POKEYS56U.pokeys56U_USBDevice()
    poKeys56U.open()
    print '--USER ID SET / GET --------'
    userID = poKeys56U.GetUserID(0x02)
    print ('GET : ', userID)
    userID = poKeys56U.SetUserID(0x0A, 0x03)
    print ('SET : ', userID)
    userID = poKeys56U.GetUserID(0x04)
    print ('GET : ', userID)
    poKeys56U.close()

def test_board_revision():
    poKeys56U = POKEYS56U.pokeys56U_USBDevice()
    poKeys56U.open()
    print '-- Serial --'
    serial = poKeys56U.GetDeviceIDEx(0x01)
    print serial
    print poKeys56U.list2bytes(serial)
    
    print '-- Load Status --'
    SystemLoad = poKeys56U.GetSystemLoadStatus(0x05)
    print "System Load: %d /100" % (SystemLoad[0])
    
    print '-- Build Date --'
    builddate = poKeys56U.GetReadBuildDate(0x06)
    print builddate
    poKeys56U.close()

def test_information():
    """ Information DeviceUSB """
    poKeys56U = POKEYS56U.pokeys56U_USBDevice()
    poKeys56U.open()
    poKeys56U.info()
    print 'Manufacturer: ', poKeys56U.getManufactureName()
    print 'Product: ', poKeys56U.getProductName()
    print 'Serial: ', poKeys56U.getSerialNumber()
    poKeys56U.close()

def main():

    while 1:
        print('1 - Setup')
        print('O - Output / Blink LED') # OK
        print('I - Input')
        print('R - Rising edge')
        print('F - Falling edge')
        print('H - High detect')
        print('L - Low detect')
        print('G - GPIO Function') # OK
        print('U - User ID') # OK
        print('B - Board revision') # OK
        print('V - Version') # OK
        print('Z - Information Device USB') #OK
        print('X - eXit') # OK
        command = raw_input('Enter your choice: ').upper()
    
        if command.startswith('1'):
            test_setup()
        elif command.startswith('O'):
            test_output()
        elif command.startswith('I'):
            test_input()
        elif command.startswith('R'):
            test_rising()
        elif command.startswith('F'):
            test_falling()
        elif command.startswith('H'):
            test_high()
        elif command.startswith('L'):
            test_low()
        elif command.startswith('G'):
            test_gpio_function()
        elif command.startswith('U'):
            test_user_id()
        elif command.startswith('B'):
            test_board_revision()
        elif command.startswith('Z'):
            test_information()
        elif command.startswith('V'):
            print('pyPokeys56U Version', version)
        elif command.startswith('X'):
            break
    
#allows use as a module or stand-alone script
if __name__ == '__main__': main()
